using System.Web.Mvc;
using Ninject;
using WroxGallery.Models.Interfaces;

namespace WroxGallery.Actions
{
    public class TitleFilterAttribute : ActionFilterAttribute
    {
        private IConfigurationRepository _repository;
        [Inject]
        public IConfigurationRepository Repository
        {
            get { return _repository; }
            set { _repository = value; }
        }

        public string Title { get; set; }

        public TitleFilterAttribute(string title)
        {
            Title = title;
        }

        public TitleFilterAttribute(string title, IConfigurationRepository repository)
        {
            _repository = repository;
            Title = title;
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            ViewResult result = filterContext.Result as ViewResult;
            if (result != null)
            {
                string galleryName = _repository.GalleryName;
                result.ViewData["GalleryName"] = galleryName;
                if (result.ViewData["Title"] == null)
                    result.ViewData["Title"] = "";
                else
                    result.ViewData["Title"] += " - ";
                result.ViewData["Title"] += galleryName + " - " + Title;
            }
        }
    }
}